package com.demo.test.other;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * 对 交通事故、交通拥堵、占道施工、排队超限、机动车异常停车  事件做数据聚合
 * 保留中间数据，汇聚展示开始时间、结束时间
 * @author gy
 * @date 2023/10/10
 */
public class CarInterval {
    /**
     * 时间间隔
     */
    private static final Integer interval = 1;

    public List<EventDo> initData(){
        List<EventDo> eventDoList = new ArrayList<EventDo>();
        eventDoList.add(new EventDo(9, "1024", 18));
        eventDoList.add(new EventDo(9, "1024", 19));
        eventDoList.add(new EventDo(9, "1024", 21));
        eventDoList.add(new EventDo(9, "1024", 23));
        eventDoList.add(new EventDo(9, "1024", 24));
        eventDoList.add(new EventDo(9, "1024", 25));
        eventDoList.add(new EventDo(9, "1024", 26));
        eventDoList.add(new EventDo(9, "1024", 27));
        eventDoList.add(new EventDo(9, "1024", 29));
        eventDoList.add(new EventDo(9, "1024", 30));
        eventDoList.add(new EventDo(9, "1024", 31));
        eventDoList.add(new EventDo(9, "1024", 35));
        eventDoList.add(new EventDo(9, "1024", 36));
        return eventDoList;
    }
    @Test
    public void m1(){
        List<List<Integer>> allGroupList = new ArrayList<>();
        List<Integer> singleGroupList = new ArrayList<>();
        Integer before = null;
        List<EventDo> eventDoList = initData();
        for (EventDo eventDo : eventDoList) {
            Integer num = eventDo.getNum();
            if (singleGroupList.isEmpty()) {
                before = num;
                singleGroupList.add(num);
            }else {
                int abs = Math.abs(num - before);
                if (abs <= interval) {
                    // 小于等于 间隔  正常添加
                    singleGroupList.add(num);
                    before = num;
                } else {
                    // 超过 间隔  将上一组的数据添加到大组内，并清空单个组
                    allGroupList.add(singleGroupList);
                    singleGroupList = new ArrayList<>();
                    singleGroupList.add(num);
                    before = num;
                }
            }
        }

        System.out.println(allGroupList);
    }

    @Test
    public void m2(){
        List<List<Integer>> allGroupList = new ArrayList<>();
        List<Integer> singleGroupList = new ArrayList<>();
        Integer before = null;
        List<EventDo> eventDoList = initData();
        for (int i = 0; i < eventDoList.size(); i++) {
            EventDo eventDo = eventDoList.get(i);
            Integer num = eventDo.getNum();
            if (singleGroupList.isEmpty()) {
                before = num;
                singleGroupList.add(num);
            }else {
                int abs = Math.abs(num - before);
                if (abs <= interval) {
                    // 小于等于 间隔  正常添加
                    singleGroupList.add(num);
                    before = num;
                } else {
                    // 超过 间隔  将上一组的数据添加到大组内，并清空单个组
                    allGroupList.add(singleGroupList);
                    singleGroupList = new ArrayList<>();
                    singleGroupList.add(num);
                    before = num;
                }
            }
            if (i == eventDoList.size() - 1){
                allGroupList.add(singleGroupList);
            }
        }

        System.out.println(allGroupList);
    }


    public void m3(){
        List<List<Integer>> allGroupList = new ArrayList<>();
        List<Integer> singleGroupList = new ArrayList<>();
        Integer before = null;
        List<EventDo> eventDoList = initData();
        for (int i = 0; i < eventDoList.size(); i++) {
            EventDo eventDo = eventDoList.get(i);
            Integer num = eventDo.getNum();
            if (singleGroupList.isEmpty()) {
                before = num;
                singleGroupList.add(num);
            }else {
                int abs = Math.abs(num - before);
                if (abs <= interval) {
                    // 小于等于 间隔  正常添加
                    singleGroupList.add(num);
                    before = num;
                } else {
                    // 超过 间隔  将上一组的数据添加到大组内，并清空单个组
                    allGroupList.add(singleGroupList);
                    singleGroupList = new ArrayList<>();
                    singleGroupList.add(num);
                    before = num;
                }
            }
            if (i == eventDoList.size() - 1){
                allGroupList.add(singleGroupList);
            }
        }

        System.out.println(allGroupList);
    }

    public List<List<Integer>> handleGroupData(List<List<Integer>> allGroupList){
        List<List<Integer>> handleAllGroupList = new ArrayList<>();
        for (List<Integer> group : allGroupList) {
            ArrayList<Integer> firstAndLast = new ArrayList<>();
            Integer first = group.get(0);
            Integer last = group.get(group.size() - 1);
            firstAndLast.add(first);
            firstAndLast.add(last);
            handleAllGroupList.add(firstAndLast);
        }
        return handleAllGroupList;
    }


}
